import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { BladeData } from "../../../../shared/blade/blade-data";
import { Blade } from "../../../../shared/blade/blade.model";
import { Command } from "../../../../shared/toolbar.service";
import { DialogResult } from "../../../../shared/dialog.service";
import { Observable } from "rxjs/Observable";
import {FarmService} from '../../../../service/farm.service';
import {Farm} from '../../../../entity/farm.model';
import {PhaseService} from '../../../../service/phase.service';
import {Phase} from '../../../../entity/phase.model';

import { CalculateCategory } from "../../../../entity/calculate-category.model";
import { CalculateExpression } from "../../../../entity/calculate-expression.model";
import { CalculateExpressionEditComponent } from "../calculate-expression-edit/calculate-expression-edit.component";
import { CalculateExpressionService } from '../../../../service/calculate-expression.service';

@Component({
  selector: 'app-calculate-expression-list',
  templateUrl: './calculate-expression-list.component.html',
  styleUrls: ['./calculate-expression-list.component.css']
})
export class CalculateExpressionListComponent extends BladeContainerBase<CalculateExpression> {
  public calculateExpressions: CalculateExpression[] = [];
  public original: CalculateExpression[] = [];
  constructor(
    private expressionService: CalculateExpressionService,
  ) {
    super();
  }

  ngOnInit() {
    this.commands = [
      // new Command("add", "添加", "fa fa-plus", "setofkey:insert", blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
      new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
      // new Command("delete", "删除", "fa fa-trash-o", "setofkey:delete", blade => this.exeDelete(blade), blade => this.canDelete(blade))
    ];
    super.ngOnInit();
  }

  /**
   * 初始化数据
   *
   *
   * @memberof CircuitListComponent
   */
  initData(): any | Observable<any> | Promise<any> {
    let farmId: number;
    let phaseId: number;
    if (this.parentEntity.hasOwnProperty('farmCode')) {
      farmId = (this.parentEntity as Farm).farmId;
      phaseId = 0;
    } else if (this.parentEntity.hasOwnProperty('phaseCode')) {
      farmId = (this.parentEntity as Phase).farmId;
      phaseId = (this.parentEntity as Phase).phaseId;
    }
    this.expressionService.getExpressionByFarmId(farmId, phaseId)
      .then(res => {
        if (res) {
          this.original = [...res];
          this.calculateExpressions = [...this.original];
        }
      })
      .catch(error => { this.bladeData.error = error })
      .then(res => this.bladeData.isLoading = false);
  }

  exeAdd(blend: Blade): void {
    let data: BladeData = new BladeData();
    data.id = "ExpressionCreate";
    data.title = "新建表达式";
    data.isClosingDisabled = false;
    let createCircuitBlade = this.bladeService.createBlade(CalculateExpressionEditComponent, data, true);
    this.bladeService.showBlade(createCircuitBlade, this.currentBlade);
  }

  canExeAdd(blend: Blade): boolean {
    return true;
  }

  exeEdit(blend: Blade): void {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = "ExpressionEdit";
      data.title = this.currentEntity.expression;
      let editCircuitBlade = this.bladeService.createBlade(CalculateExpressionEditComponent, data, false, this.currentEntity);
      this.currentBlade.bladeService.showBlade(editCircuitBlade, this.currentBlade);
    }
  }

  canExeEdit(blend?: Blade): boolean {
    return this.currentEntity != null;
  }

  // exeDelete(blend: Blade): void {
  //   let selectedExpression = this.getSelected();
  //   if (selectedExpression.length > 0) {
  //     this.dialogService
  //       .showConfirmationDialog("确认删除", "是否要删除选中项？")
  //       .subscribe(dialogResult => {
  //         if (dialogResult === DialogResult.Ok) {
  //           this.currentBlade.bladeData.isLoading = true;
  //           this.expressionService.batchDelete(selectedExpression.map(o => o.expressionId))
  //             .then(res => {
  //               if (res) {
  //                 this.refresh(undefined);
  //               }
  //             })
  //             .catch(error => this.bladeData.error = error)
  //             .then(res => this.bladeData.isLoading = false);
  //         }
  //       });

  //   }
  // }

  // canDelete(blend?: Blade): boolean {
  //   return this.getSelected().length > 0;
  // }

  onSeletedChange(expression: CalculateExpression) {
    if (this.currentBlade.childrenCanDeactivate()) {
      this.currentEntity = expression;
      if (this.parentEntity.hasOwnProperty('farmCode')) {
        this.currentEntity.farmId = (this.parentEntity as Farm).farmId;
        this.currentEntity.phaseId = 0;
      } else if (this.parentEntity.hasOwnProperty('phaseCode')) {
        this.currentEntity.farmId = (this.parentEntity as Phase).farmId;
        this.currentEntity.phaseId = (this.parentEntity as Phase).phaseId;
      }
    }
  }

  /**
    * 单选
    *
    * @param {any} event
    * @param {any} key
    *
    * @memberof DicKeyListComponent
    */
  // onItemCheack(event: HTMLInputElement, key: CalculateExpression) {
  //   key.isChecked = event.checked;
  // }

  /**
  * 全选/全不选
  *
  * @param {HTMLInputElement} event
  *
  * @memberof DicKeyListComponent
  */
  // onAllCheack(event: HTMLInputElement) {
  //   if (this.calculateExpressions.length > 0) {
  //     for (let i = 0; i < this.calculateExpressions.length; i++) {
  //       this.calculateExpressions[i].isChecked = event.checked;
  //     }
  //   }
  // }
  /**
  * 获得选中项
  *
  * @returns {Setofkey[]}
  *
  * @memberof CircuitListComponent
  */
  // getSelected(): CalculateExpression[] {
  //   if (this.calculateExpressions.length > 0) {
  //     let filter = this.calculateExpressions.filter(ref => ref.isChecked)
  //     return filter == null ? [] : filter;
  //   }
  //   return [];
  // }


}



// WEBPACK FOOTER //
// ./src/app/module/basic/calculate-expression/calculate-expression-list/calculate-expression-list.component.ts
